<?php
class Application_Model_Tag_Item_Assocs extends Zend_Db_Table_Abstract {
	protected $_name = 'tag_item_assoc';
	protected $_primary = array('tag_id', 'item_id', 'item_name');
	
	public function getMetadata()
	{
		if(empty($this->_metadata))
		{
			$this->_setupMetadata();
		}
		return $this->_metadata;
	}
	
	/**
	 * 
	 * find many rows
	 * @param Int $tag_id
	 */
	public function _find($tag_id) {
        try 
        {
            $row = parent::find($tag_id)->current();
            return Utility_Converter::arrayToObject($row->toArray());
        }catch(Zend_Db_Exception $ex) 
        {
            throw $ex;
        }
	}
	
	/**
	 * 
	 * Enter description here ...
	 * @param Object $object
	 */
	public function _insert($object) {
		try {
			$data = Utility_Converter::objectToArray($object);
			return parent::insert($data);
		}
		catch (Zend_Db_Exception $ex) {
			throw $ex;
		}
	}
	
	public function _delete($article_id) {
		try 
		{
			// This table delete through param
			$param = "article_id:" . $article_id;
			$where = parent::getAdapter()
					->quoteInto('params = ?', $param);
			$rows = parent::delete($where);
			return $rows;
		}
		catch (Zend_Db_Exception $ex) {
			throw $ex;
		}
	}
	
	/**
	 * 
	 * Update tag_article_assoc data
	 * @param Object $object
	 */
	public function _update($object){
		try {
			$adapter = parent::getAdapter();
			$where = $adapter->quoteInto('tag_id = ?', $object->tag_id);
			$data = Utility_Converter::objectToArray($object);
			return parent::update($data, $where);
		} catch(Zend_Db_Exception $ex) {
			throw $ex;		
		}
	}
	
	/* Extend function */
	
	/**
	 * 
	 * Find tags that assocs with inputed article. 
	 */
	public function FindTags($article_id) {
		$param = "article_id:" . $article_id;
		$select = parent::select();
		$sql = $select->__toString();
		$adapter = parent::getAdapter();
		$where = $adapter->quoteInto("params = ?", $param);
		$sql .= " where ";
		$sql .= $where;
		$result = $adapter->query($sql);
		$rows = $result->fetchAll();
		return $rows;
	}

}